package material.observer;

/**
 * 布告板--酷热指数
 * 结合温度和湿度的指数--用于显示人的温度感受
 */
public class HeatIndexDisplay implements Observer, DisplayElement {
    private float heatIndex = 0.0f;//酷热指数
    private Subject weatherData;//由于此布告板实现了Observer接口，所以可以从WeatherData中获取改变

    public HeatIndexDisplay(Subject weatherData) {
        this.weatherData = weatherData;
        weatherData.registerObserver(this);//将自身注册为观察者
    }

    @Override
    public void display() {
        System.out.println("当前的酷热指数为：" + heatIndex);
    }

    @Override
    public void update(float temp, float humidity, float pressure) {
        this.heatIndex = this.computeHeatIndex(temp, humidity);
        this.display();
    }

    /**
     * 计算酷热指数
     *
     * @param t
     * @param rh
     * @return
     */
    private float computeHeatIndex(float t, float rh) {
        float index = (float) ((16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh) +
                (0.00941695 * (t * t)) + (0.00728898 * (rh * rh)) +
                (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) +
                (0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 *
                (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) +
                (0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) +
                0.000000000843296 * (t * t * rh * rh * rh)) -
                (0.0000000000481975 * (t * t * t * rh * rh * rh)));
        return index;
    }

}
